package com.luoyi.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.util.HashSet;
import java.util.Set;

/**
 * @author tail7
 */
public class JsonUtil {

    /**
     * 从数据库中复制出来的json字段全部转小写然后改成驼峰
     */
    public static JSONObject jsonToCamelCase(String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        Set<String> keys = jsonObject.keySet();
        Set<String> needRemove = new HashSet<>(keys);
        needRemove.forEach(key -> {
            String lowerCase = key.toLowerCase();
            String formatKey = StrUtil.toCamelCase(lowerCase);
            jsonObject.put(formatKey, jsonObject.get(key));
            jsonObject.remove(key);
        });
        return jsonObject;
    }

    public static void main(String[] args) {
        JSONObject jsonObject = jsonToCamelCase("{\n" +
                "    \"ID\": 21104,\n" +
                "    \"TENANT_ID\": null,\n" +
                "    \"DIALOGUE_ID\": \"DH2024091312000638563\",\n" +
                "    \"PHONE_TYPE\": \"1\",\n" +
                "    \"DIALOGUE_TYPE\": \"2\",\n" +
                "    \"DIALOGUE_CHANNEL\": null,\n" +
                "    \"ACCOUNT_ID\": null,\n" +
                "    \"ACCOUNT_NAME\": null,\n" +
                "    \"SATISFACTION\": null,\n" +
                "    \"CUSTOMER_PHONE_NUMBER\": \"18707437105\",\n" +
                "    \"CALLER_NAME\": null,\n" +
                "    \"DIALOGUE_START_TIME\": \"2024-09-13 19:49:51\",\n" +
                "    \"DIALOGUE_END_TIME\": \"2024-09-13 19:50:10\",\n" +
                "    \"CALLER_TIME\": 19,\n" +
                "    \"QC_SCORE\": null,\n" +
                "    \"QC_STATUS\": \"1\",\n" +
                "    \"CREATED_AT\": \"2024-09-13T12:00:01.000Z\",\n" +
                "    \"CREATED_BY\": null,\n" +
                "    \"UPDATED_AT\": \"2024-09-13T12:00:01.000Z\",\n" +
                "    \"UPDATED_BY\": null,\n" +
                "    \"DELETED\": 0,\n" +
                "    \"ACCOUNT_NO\": null,\n" +
                "    \"DIALOGUE_CONTINUE_TIME\": \"00:00:19\",\n" +
                "    \"ACCEPT_ACCOUNT_ID\": null,\n" +
                "    \"ACCEPT_ACCOUNT_NAME\": null,\n" +
                "    \"DEPARTMENT_ID\": null,\n" +
                "    \"CREATED_BY_NAME\": null,\n" +
                "    \"BELONG_TYPE\": null,\n" +
                "    \"OUTBOUND_TASK_NAME\": null,\n" +
                "    \"DEPARTMENT_CODE\": null,\n" +
                "    \"SYBBP\": null\n" +
                "}");
        System.out.println(jsonObject.toJSONString());
    }
}
